//
//  HRTabBarViewController.m
//  Hear
//
//  Created by mac on 2017/6/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRTabBarViewController.h"
#import "HRHearViewController.h"
#import "HRMineViewController.h"
#import "HRDynamicRootViewController.h"
#import "HRMessageRootViewController.h"
#import "HearNavigationController.h"
#import "HRFMDBDynamicNotifacationTool.h"
#import "HRPostDynamicViewController.h"
#import "HRRecordAudioViewController.h"
#import "PopMenu.h"
#import "HRFunChatRootViewController.h"
#import "HRCircleOfFirendsViewController.h"
#import "HRFollowDynamicViewController.h"
#import "HRRecommendViewController.h"
@interface HRTabBarViewController ()<UINavigationControllerDelegate>
@property (nonatomic, strong) PopMenu *popMenu;

@end

@implementation HRTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self setUpChildVC];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(newMesageNotifi) name:@"newMesageNotifi" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTabItemBageCountNotifi) name:@"reloadTabItemBageCountNotifi" object:nil];
    
    [[NSUserDefaults standardUserDefaults] setInteger:300 forKey:@"cacheTime"];

}








#pragma mark -初始化所有控制器

- (void)setUpChildVC {
    
//    HRHearViewController *homeVC = [[HRHearViewController alloc] init];
    HRRecommendViewController *homeVC = [[HRRecommendViewController alloc] init];
    [self setChildVC:homeVC title:@"留声机" image:@"默认-留声机" selectedImage:@"默认-留声机-select"];
    
//    HRDynamicRootViewController *fishpidVC = [[HRDynamicRootViewController alloc] init];
//    [self setChildVC:fishpidVC title:@"点滴" image:@"默认-点滴" selectedImage:@"默认-点滴-select"];
    
    HRFunChatRootViewController *funChatVC = [HRFunChatRootViewController new];
    [self setChildVC:funChatVC title:@"趣聊" image:@"默认-匹配" selectedImage:@"默认-匹配-select"];

    HRFollowDynamicViewController *messageVC = [[HRFollowDynamicViewController alloc] init];
    [self setChildVC:messageVC title:@"广场" image:@"默认-消息" selectedImage:@"默认-消息-select"];
    
    HRMineViewController *myVC = [[HRMineViewController alloc] init];
    [self setChildVC:myVC title:@"我的" image:@"默认-我的" selectedImage:@"默认-我的-select"];
    
}

- (void) setChildVC:(UIViewController *)childVC title:(NSString *) title image:(NSString *) image selectedImage:(NSString *) selectedImage {
    
    childVC.tabBarItem.title = title;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    dict[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childVC.tabBarItem setTitleTextAttributes:@{ NSForegroundColorAttributeName:MainColor} forState:UIControlStateSelected];
    [childVC.tabBarItem setTitleTextAttributes:dict forState:UIControlStateNormal];
    childVC.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVC.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    HearNavigationController *nav = [[HearNavigationController alloc] initWithRootViewController:childVC];
    nav.delegate = self;
    [self addChildViewController:nav];

}


- (void)animationWithIndex:(NSInteger) index {
    NSMutableArray * tabbarbuttonArray = [NSMutableArray array];
    for (UIView *tabBarButton in self.tabBar.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            [tabbarbuttonArray addObject:tabBarButton];
        }
    }
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= YES;
    pulse.fromValue= [NSNumber numberWithFloat:0.7];
    pulse.toValue= [NSNumber numberWithFloat:1.3];
    [[tabbarbuttonArray[index] layer]
     addAnimation:pulse forKey:nil];
}







-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self newMesageNotifi];
}

- (void)newMesageNotifi{
    NSArray *conversations = [[EMClient sharedClient].chatManager getAllConversations];
    NSInteger unreadCount = 0;
    for (EMConversation *conversation in conversations) {
        unreadCount += conversation.unreadMessagesCount;
    }
//    unreadCount += [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:@[@"10"]];
//    unreadCount += [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:@[@"1",@"2",@"5"]];
//    unreadCount += [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:@[@"9",@"12"]];

    
    NSInteger dynamicUnreadCount= [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:@[@"1",@"2"]];
    UITabBarItem *item = [self.tabBar.items objectAtIndex:2];

    if (dynamicUnreadCount){
        item.badgeValue = [NSString stringWithFormat:@"%ld",(long)dynamicUnreadCount];
    }else{
        item.badgeValue = nil;
    }
    
    unreadCount += [[HRFMDBDynamicNotifacationTool sharedInstance] unReadCount:nil];
    UITabBarItem *item2 = [self.tabBar.items objectAtIndex:3];
    if (unreadCount){
        item2.badgeValue = [NSString stringWithFormat:@"%ld",(long)unreadCount];
    }else{
        item2.badgeValue = nil;
    }

}

- (void)reloadTabItemBageCountNotifi{
    [self newMesageNotifi];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
